package com.jilujia.framework;

import java.beans.PropertyEditorSupport;

import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

public class JsoupStringEscapeEditor extends PropertyEditorSupport {

	private boolean escapeHTML = true;

	public JsoupStringEscapeEditor() {
		super();
	}

	public JsoupStringEscapeEditor(boolean escapeHTML) {
		super();
		this.escapeHTML = escapeHTML;
	}

	public void setAsText(String text) {
		if (text == null) {
			setValue(null);
		} else {
			String value = text;
			if (escapeHTML) {
				value = Jsoup.clean(value, Whitelist.relaxed());
			}
			setValue(value);
		}
	}

	public String getAsText() {
		Object value = getValue();
		return value != null ? value.toString() : "";
	}
}
